package edu.thu.studio.ormdesigner.common;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

public class CompositeFactory {
    
    public static final int INDENT = 20;
    
    public static final int SMALL_BUTTON_WIDTH = 70;
    
    public static final int PREFERENCE_PAGE_MARGIN_TOP = 30;
    
    
    /**
     * create text
     */
    public static Text createText(AbstractDialog dialog, Composite composite, String title, boolean imeOn,
        boolean indent) {
        return createText(dialog, composite, title, 1, imeOn, indent);
    }
    
    public static Text createText(AbstractDialog dialog, Composite composite, String title, int span, boolean imeOn,
        boolean indent) {
        return createText(dialog, composite, title, span, -1, imeOn, indent);
    }
    
    public static Text createText(AbstractDialog dialog, Composite composite, String title, int span, int width,
        boolean imeOn, boolean indent) {
        return createText(dialog, composite, title, span, width, SWT.BORDER, imeOn, indent);
    }
    
    public static Text createText(AbstractDialog dialog, Composite composite, String title, int span, int width,
        int style, boolean imeOn, boolean indent) {
        if (title != null) {
            Label label = new Label(composite, SWT.NONE);
            if (indent) {
                GridData labelGridData = new GridData();
                labelGridData.horizontalAlignment = SWT.LEFT;
                label.setLayoutData(labelGridData);
            }
            
            label.setText(title);
        }
        
        GridData textGridData = new GridData();
        textGridData.horizontalSpan = span;
        if (indent) {
            textGridData.horizontalIndent = INDENT;
        }
        
        if (width > 0) {
            textGridData.widthHint = width;
            
        } else {
            textGridData.horizontalAlignment = GridData.FILL;
            textGridData.grabExcessHorizontalSpace = true;
        }
        
        Text text = new Text(composite, style);
        text.setLayoutData(textGridData);
        
        ListenerAppender.addTextListener(text, dialog, imeOn);
        
        return text;
    }
    
    /**
     * create textarea
     */
    
    public static Text createTextArea(AbstractDialog dialog, Composite composite, String title, int width, int height,
        int span, boolean imeOn) {
        return createTextArea(dialog, composite, title, width, height, span, true, imeOn, true);
    }
    
    public static Text createTextArea(AbstractDialog dialog, Composite composite, String title, int width, int height,
        int span, boolean imeOn, boolean indent) {
        return createTextArea(dialog, composite, title, width, height, span, true, imeOn, indent);
    }
    
    public static Text createTextArea(AbstractDialog dialog, Composite composite, String title, int width, int height,
        int span, boolean selectAll, boolean imeOn, boolean indent) {
        if (title != null) {
            Label label = new Label(composite, SWT.NONE);
            
            GridData labelGridData = new GridData();
            labelGridData.verticalAlignment = SWT.TOP;
            labelGridData.horizontalAlignment = SWT.LEFT;
            
            label.setLayoutData(labelGridData);
            
            label.setText(title);
        }
        
        GridData textAreaGridData = new GridData();
        textAreaGridData.heightHint = height;
        textAreaGridData.horizontalSpan = span;
        
        if (width > 0) {
            textAreaGridData.widthHint = width;
        } else {
            textAreaGridData.horizontalAlignment = GridData.FILL;
            textAreaGridData.grabExcessHorizontalSpace = true;
        }
        
        if (title != null && indent) {
            textAreaGridData.horizontalIndent = INDENT;
        }
        
        Text text = new Text(composite, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
        text.setLayoutData(textAreaGridData);
        
        // 不做非空判断
        // ListenerAppender.addTextAreaListener(text, dialog, selectAll, imeOn);
        
        return text;
    }
    
    /**
     * num text
     */
    
    public static Text createNumText(AbstractDialog dialog, Composite composite, String title) {
        return createNumText(dialog, composite, title, -1);
    }
    
    public static Text createNumText(AbstractDialog dialog, Composite composite, String title, boolean indent) {
        return createNumText(dialog, composite, title, 1, -1, indent);
    }
    
    public static Text createNumText(AbstractDialog dialog, Composite composite, String title, int width) {
        return createNumText(dialog, composite, title, 1, width);
    }
    
    public static Text createNumText(AbstractDialog dialog, Composite composite, String title, int span, int width) {
        return createNumText(dialog, composite, title, span, width, false);
    }
    
    public static Text createNumText(AbstractDialog dialog, Composite composite, String title, int span, int width,
        boolean indent) {
        return createText(dialog, composite, title, span, width, SWT.BORDER | SWT.RIGHT, false, indent);
    }
    
    /**
     * create combo
     */
    public static Combo createReadOnlyCombo(AbstractDialog dialog, Composite composite, String title) {
        return createReadOnlyCombo(dialog, composite, title, 1);
    }
    
    public static Combo createReadOnlyCombo(AbstractDialog dialog, Composite composite, String title, int span) {
        return createReadOnlyCombo(dialog, composite, title, span, -1);
    }
    
    public static Combo createReadOnlyCombo(AbstractDialog dialog, Composite composite, String title, int span,
        int width) {
        GridData gridData = new GridData();
        gridData.horizontalSpan = span;
        
        if (title != null) {
            gridData.horizontalIndent = INDENT;
            
            Label label = new Label(composite, SWT.LEFT);
            
            GridData labelGridData = new GridData();
            labelGridData.horizontalAlignment = SWT.LEFT;
            label.setLayoutData(labelGridData);
            label.setText(title);
        }
        
        if (width > 0) {
            gridData.widthHint = width;
            
        } else {
            gridData.horizontalAlignment = GridData.FILL;
            gridData.grabExcessHorizontalSpace = true;
        }
        
        Combo combo = new Combo(composite, SWT.READ_ONLY);
        combo.setLayoutData(gridData);
        
        ListenerAppender.addComboListener(combo, dialog, false);
        
        return combo;
    }
    
    /**
     * create table
     */
    public static Table createTable(Composite composite, int height, int span) {
        return createTable(composite, height, span, false);
    }
    
    public static Table createTable(Composite composite, int height, int span, boolean multi) {
        GridData gridData = new GridData();
        gridData.horizontalSpan = span;
        gridData.heightHint = height;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        
        int style = SWT.SINGLE;
        if (multi) {
            style = SWT.MULTI;
        }
        
        Table table = new Table(composite, style | SWT.BORDER | SWT.FULL_SELECTION);
        table.setLayoutData(gridData);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        
        return table;
    }
    
    /**
     * create column
     */
    public static TableColumn createTableColumn(Table table, String title) {
        return createTableColumn(table, title, -1);
    }
    
    public static TableColumn createTableColumn(Table table, String title, int width) {
        return createTableColumn(table, title, width, SWT.LEFT);
    }
    
    public static TableColumn createTableColumn(Table table, String title, int width, int align) {
        TableColumn column = new TableColumn(table, align);
        
        column.setText(title);
        
        if (width >= 0) {
            column.setWidth(width);
        } else {
            column.pack();
        }
        
        return column;
    }
    
    /**
     * create child
     */
    public static Composite createChildComposite(Composite parent, int span, int numColumns) {
        return createChildComposite(parent, -1, span, numColumns);
    }
    
    public static Composite createChildComposite(Composite parent, int height, int span, int numColumns) {
        return createChildComposite(parent, height, -1, span, numColumns);
    }
    
    public static Composite createChildComposite(Composite parent, int height, int width, int span, int numColumns) {
        Composite composite = new Composite(parent, SWT.NONE);
        
        GridData gridData = new GridData();
        gridData.horizontalSpan = span;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessHorizontalSpace = true;
        
        if (height >= 0) {
            gridData.heightHint = height;
        }
        
        if (width >= 0) {
            gridData.widthHint = width;
        }
        
        composite.setLayoutData(gridData);
        
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginWidth = 0;
        gridLayout.numColumns = numColumns;
        
        composite.setLayout(gridLayout);
        
        return composite;
    }
    
    /**
     * create button
     */
    public static Button createSmallButton(Composite composite, String text) {
        return createButton(composite, text, -1, SMALL_BUTTON_WIDTH);
    }
    
    public static Button createButton(Composite composite, String text, int span, int width) {
        GridData gridData = new GridData();
        gridData.widthHint = width;
        
        if (span != -1) {
            gridData.horizontalSpan = span;
        }
        
        Button button = new Button(composite, SWT.NONE);
        button.setText(text);
        button.setLayoutData(gridData);
        
        return button;
    }
    
    /**
     * filler
     */
    public static void filler(Composite composite, int span) {
        filler(composite, span, -1);
    }
    
    public static void filler(Composite composite, int span, int width) {
        GridData gridData = new GridData();
        gridData.horizontalSpan = span;
        gridData.heightHint = 1;
        
        if (width > 0) {
            gridData.widthHint = width;
        }
        
        Label label = new Label(composite, SWT.NONE);
        label.setLayoutData(gridData);
    }
    
    /**
     * create checkbox
     */
    public static Button createCheckbox(AbstractDialog dialog, Composite composite, String title, boolean indent) {
        return createCheckbox(dialog, composite, title, indent, -1);
    }
    
    public static Button createCheckbox(AbstractDialog dialog, Composite composite, String title, boolean indent,
        int span) {
        Button checkbox = new Button(composite, SWT.CHECK);
        checkbox.setText(title);
        
        GridData gridData = new GridData();
        
        if (span != -1) {
            gridData.horizontalSpan = span;
        }
        if (indent) {
            gridData.horizontalIndent = INDENT;
        }
        
        checkbox.setLayoutData(gridData);
        
        // ListenerAppender.addCheckBoxListener(checkbox, dialog);
        
        return checkbox;
    }
    
    /*
     * create group
     */
    public static Group createGroup(Composite parent, String title, int span, int numColumns) {
        return createGroup(parent, title, span, numColumns, 15);
    }
    
    public static Group createGroup(Composite parent, String title, int span, int numColumns, int margin) {
        GridData groupGridData = new GridData();
        groupGridData.horizontalAlignment = GridData.FILL;
        groupGridData.grabExcessHorizontalSpace = true;
        groupGridData.verticalAlignment = GridData.FILL;
        groupGridData.grabExcessVerticalSpace = true;
        groupGridData.horizontalSpan = span;
        
        GridLayout groupLayout = new GridLayout();
        groupLayout.marginWidth = margin;
        groupLayout.marginHeight = margin;
        groupLayout.numColumns = numColumns;
        
        Group group = new Group(parent, SWT.NONE);
        group.setText(title);
        group.setLayoutData(groupGridData);
        group.setLayout(groupLayout);
        
        return group;
    }
    
    /**
     * create radio
     */
    public static Button createRadio(AbstractDialog dialog, Composite composite, String title) {
        return createRadio(dialog, composite, title, -1);
    }
    
    public static Button createRadio(AbstractDialog dialog, Composite composite, String title, int span) {
        return createRadio(dialog, composite, title, span, false);
    }
    
    public static Button createRadio(AbstractDialog dialog, Composite composite, String title, int span, boolean indent) {
        Button radio = new Button(composite, SWT.RADIO);
        radio.setText(title);
        
        GridData gridData = new GridData();
        
        if (span != -1) {
            gridData.horizontalSpan = span;
        }
        
        if (indent) {
            gridData.horizontalIndent = INDENT;
        }
        
        radio.setLayoutData(gridData);
        
        // ListenerAppender.addCheckBoxListener(radio, dialog);
        
        return radio;
    }
    
}
